package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.user.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@Api(tags = "购物车接口")
@RequestMapping("/user/shoppingCart")
@RestController("userShoppingCartController")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 查看购物车
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> select(){
        List<ShoppingCart> shoppingCartList =  shoppingCartService.select();
        return Result.success(shoppingCartList);
    }


    /**
     * 添加购物车
     * @param shoppingCartDto
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result insert(@RequestBody ShoppingCartDTO shoppingCartDto){
        shoppingCartService.insert(shoppingCartDto);
        return Result.success();
    }


    /**
     * 清空购物车
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result delete(){
        shoppingCartService.deleteAll();
        return Result.success();
    }

    /**
     * 购物车物品减一
     * @param shoppingCartDto
     * @return
     */
    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result deleteById(@RequestBody ShoppingCartDTO shoppingCartDto){
        shoppingCartService.deleteById(shoppingCartDto);
        return Result.success();
    }


}
